package ch.hearc.ig.android.crudpersonnes.utils;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import ch.hearc.ig.android.crudpersonnes.business.Personne;
import ch.hearc.ig.android.crudpersonnes.screen.LoginActivity;

/**
 * Helper class for providing sample content for user interfaces created by
 * Android template wizards.
 * <p>
 */
public class PersonneUtil {
	public static ArrayList<Personne> stringJsonToListPersonne(String jsonString) {
		ArrayList<Personne> personnes = new ArrayList<Personne>();
		JSONObject jsonObject;
		JSONArray jsonArray;

		try {
			jsonArray = new JSONArray(jsonString);
			for (int i = 0; i < jsonArray.length(); i++) {
				jsonObject = jsonArray.getJSONObject(i);
				String id = jsonObject.getString("id");
				String nom = jsonObject.getString("nom");
				String prenom = jsonObject.getString("prenom");
				String adresse = jsonObject.getString("adresse");
				String ville = jsonObject.getString("ville");

				personnes.add(new Personne(Long.parseLong(id), nom, prenom,
						adresse, ville));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return personnes;
	}

	public static String constructUrl(String... params) {
		StringBuilder returnUrl = new StringBuilder();
		
		// Research
		Log.i("PersonneUtil", params[0]);
		if (params[0].equalsIgnoreCase("ServiceR")) {
			returnUrl.append(params[0]);
			// By ID
			if (!params[1].isEmpty()) {
				returnUrl.append("?id=");
				returnUrl.append(params[1]);
			}
			// By searchWord
			if (!params[6].isEmpty()) {
				returnUrl.append("?searchWord=");
				returnUrl.append(params[6]);
			}
		}

		// Create
		if (params[0].equals("ServiceC")) {
			returnUrl.append(params[0]);
			returnUrl.append("?nom=");
			returnUrl.append(params[2]);
			returnUrl.append("&prenom=");
			returnUrl.append(params[3]);
			returnUrl.append("&adresse=");
			returnUrl.append(params[4]);
			returnUrl.append("&ville=");
			returnUrl.append(params[5]);
		}

		// Update
		if (params[0].equals("ServiceU")) {
			returnUrl.append(params[0]);
			returnUrl.append("?id=");
			returnUrl.append(params[1]);
			returnUrl.append("&nom=");
			returnUrl.append(params[2]);
			returnUrl.append("&prenom=");
			returnUrl.append(params[3]);
			returnUrl.append("&adresse=");
			returnUrl.append(params[4]);
			returnUrl.append("&ville=");
			returnUrl.append(params[5]);
		}

		// Delete
		if (params[0].equals("ServiceD")) {
			returnUrl.append(params[0]);
			returnUrl.append("?id=");
			returnUrl.append(params[1]);
		}
		
		// Session
		if (params[0].equals("ServiceSession")){
			returnUrl.append(params[0]);
			if(params[9].equals("login")){
				returnUrl.append("?action=");
				returnUrl.append(params[9]);
				returnUrl.append("&user=");
				returnUrl.append(params[7]);
				returnUrl.append("&password=");
				returnUrl.append(params[8]);
			}
			if(params[9].equalsIgnoreCase("Logout")){
				returnUrl.append("?action=");
				returnUrl.append(params[9]);
			}
			
		}
		// add the Session id to the url
		if (LoginActivity.SESSION_ID!=null){
			if (returnUrl.length()>8){
				returnUrl.append("&");	
			}
			else{
				returnUrl.append("?");
			}
			returnUrl.append("sessionId=");
			returnUrl.append(LoginActivity.SESSION_ID);	
		}
		Log.i("PersonneUtil", returnUrl.toString());
		return returnUrl.toString();
	}

	public static String[] createParamlist(String service, String id,
			String nom, String prenom, String adresse, String ville,
			String searchWord, String user, String password, String action) {
		String[] params = new String[10];
		for (int i = 0; i < 10; i++) {
			params[i] = new String();
		}
		if (service != null) {
			params[0] = service;
		}
		if (id != null) {
			params[1] = id;
		}
		if (nom != null) {
			params[2] = nom;
		}
		if (prenom != null) {
			params[3] = prenom;
		}
		if (adresse != null) {
			params[4] = adresse;
		}
		if (ville != null) {
			params[5] = ville;
		}
		if (searchWord != null) {
			params[6] = searchWord;
		}
		if (user != null) {
			params[7] = user;
		}
		if (password != null) {
			params[8] = password;
		}
		if (action != null) {
			params[9] = action;
		}
		
		
		return params;
	}

}
